package ar.com.quickmart.connectivity;

import java.util.List;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

import ar.com.quickmart.classes.Promo;
import ar.com.quickmart.classes.Supermarket;

public class WebServiceProduct extends WebServiceBase {

	/**
     * 
     * getProductName
     * 
     * Obtiene el nombre de un producto a partir de su codigo de barras
     *     
     * @param barcode
     * @return
     * @throws Exception
     */
	public static String getProductName(long barcode) throws Exception {

		// Variables propias del webservice
		String methodName = "getProductName";
		String param1 = "barCode";
		Object callResult;
		String result = null;
		
		// Creo parametro con el codigo de barras user
        PropertyInfo barcodeParameter = new PropertyInfo();
        barcodeParameter.setName(param1);
        barcodeParameter.setValue(barcode);
        barcodeParameter.setType(long.class);
	    
        try {       	
        	// Obtengo el resultado y lo convierto a string
        	callResult = callWebService(methodName, barcodeParameter);
        	if(callResult != null) {
        		result = callResult.toString();
        	}
        }
        catch (Exception ex) {
        	ex.printStackTrace();
        	
        	throw ex;
        }      
        return result;        
	}
	
	public static Promo getPromotionInfo(long code) throws Exception {

		// Variables propias del webservice
		String methodName = "getPromotionInfo";
		String param1 = "code";
		Object callResult;
		Promo promo = new Promo();
		
		// Creo parametro con el codigo de barras user
        PropertyInfo codeParameter = new PropertyInfo();
        codeParameter.setName(param1);
        codeParameter.setValue(code);
        codeParameter.setType(long.class);
	    
        try {       	
        	// Obtengo el resultado y lo convierto a string
        	callResult = callWebService(methodName, codeParameter);
        	if(callResult != null) {
        		List<SoapObject> result = (List<SoapObject>) callResult;
        		
        		long id = Long.parseLong(result.get(0).getProperty("code").toString());
        		String description = result.get(0).getProperty("description").toString();
        		int count = Integer.parseInt(result.get(0).getProperty("count").toString());
        		float price = Float.parseFloat(result.get(0).getProperty("price").toString());
        		
        		SoapObject supermarket = (SoapObject) result.get(0).getProperty("superMarket");
        		long supermarketId = Long.parseLong(supermarket.getProperty("id").toString());
        		String supermarketName = supermarket.getProperty("name").toString();
        		
        		Supermarket promoSupermarket = new Supermarket(supermarketId, supermarketName, "");

        		promo.setId(id);
        		promo.setName(description);
        		promo.setCount(count);
        		promo.setPrice(price);
        		promo.setSupermarket(promoSupermarket);
        	}
        }
        catch (Exception ex) {
        	ex.printStackTrace();
        	
        	throw ex;
        }      
        return promo;        
	}
	
}
